/*
#Copyright (c) 2013, Philippe Bordron <philippe.bordron@gmail.com>
#
# This file is part of SIPPER.
#
# DCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU LESSER GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# along with SIPPER.  If not, see <http://www.gnu.org/licenses/>
*/
package combi.wip.sipper.exec;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TransSimple {

	private static String tag = "(?:([^\t]+)\t+([^\t]+)\t+([^\t]+))";
	private static String tag2 = "(?:([0-9]+))";

	public static void main(String[] args) throws IOException
	{
		String fileKsip = args[0];
		String fileAsso = args[1];
		String fileOut = args[2];
		Map<String, String> asso = new HashMap<String, String>();
		BufferedReader in = new BufferedReader (new FileReader (fileAsso));
		String ch;
		while((ch = in.readLine())!=null)
		{
			String[] c = ch.split("\t");
			asso.put(c[0], c[1]);
		}
		in.close();

		in = new BufferedReader (new FileReader (fileKsip));
		BufferedWriter out = new BufferedWriter (new FileWriter (fileOut));


		Pattern reco = Pattern.compile(tag);
		Matcher m;
		Pattern reco2 = Pattern.compile(tag2);
		Matcher m2;

		while((ch = in.readLine())!=null)
		{
			String tmp = "";
			m = reco.matcher(ch);
			if (m.find())
			{
				//System.err.println(m.group());
				m2 = reco2.matcher(m.group(3));
				tmp = m.group(1)+"\t"+m.group(2)+"\t";
				while (m2.find())
				{
					if(ch.contains("["+ m2.group(1)+","))
					{
						tmp = tmp + "["+asso.get(m2.group(1))+",";
					}
					if(ch.contains(", "+ m2.group(1)+","))
					{
						tmp = tmp + " "+asso.get(m2.group(1))+",";
					}
					if(ch.contains(", "+ m2.group(1)+"]"))
					{
						tmp = tmp + " "+asso.get(m2.group(1))+"]";
					}
//					System.err.println(m2.group(1)+ ":\""+ "["+ m2.group(1)+","+ "\" \""+ ", "+ m2.group(1)+","+ "\" \""+ ", "+ m2.group(1)+"]" + "\"");
//					System.err.println(asso.get(m2.group(1))+ ":"+ ch.contains("["+ m2.group(1)+",")+ " "+ ch.contains(", "+ m2.group(1)+",")+ " "+ ch.contains(", "+ m2.group(1)+"]"));
//					ch.replaceFirst("\\["+ m2.group(1)+",","["+asso.get(m2.group(1))+",");
//					ch.replaceFirst(", "+ m2.group(1)+",",", "+asso.get(m2.group(1))+",");
//					ch.replaceFirst(", "+ m2.group(1)+"\\]",", "+asso.get(m2.group(1))+"]");
				}
				//System.err.println("new : " + tmp);
			}
			else
			{
				tmp = ch;
			}

			out.write(tmp + "\n");
		}
		in.close();
		out.close();
	}

}
